<?php
/**
 * Created by PhpStorm.
 * User: rain
 * Date: 2018/4/5
 * Time: 下午4:51
 */

namespace App\Http\Controllers\Api;


use App\Models\BookChapter;
use App\Models\Reports;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ReportController extends AuthorizedController
{
    public function create(Request $request)
    {

        $rules = [
            'book_chapter_id' => 'required|integer',
            'en_word' => 'string',
            'opt_desc' => 'string',
            'other_desc' => 'string',
        ];

        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()){
            return $this->success();
        }

        $user = $request->apiUser;
        if (empty($user->mobile)){
            return $this->success();
        }
        /** @var BookChapter $bookChapter */
        $bookChapter = BookChapter::find($request->book_chapter_id);
        if (!empty($bookChapter)){
            $report = new Reports();
            $report->user_id = $user->id;
            $report->book_id = $bookChapter->book_id;
            $report->book_chapter_id = $bookChapter->id;
            $report->en_word = $request->get('en_word', '');
            $report->opt_desc = $request->get('opt_desc', '');
            $report->other_desc = $request->get('other_desc', '');
            $report->save();
        }

        return $this->success();

    }


}
